package com.trendit.classicpos.callback;

import android.os.Environment;

import com.trendit.classicpos.MainActivity;
import com.trendit.classicpos.core.KeyManager;
import com.trendit.classicpos.view.VirtualPosScreenView;

import java.io.UnsupportedEncodingException;

public class JavaCallback {

    private static final String TAG = "JavaCallback";
    private String sdCardPath;
    private static JavaCallback sInstance;

    private JavaCallback() {
        sdCardPath = Environment.getExternalStorageDirectory().getPath() + "/ClassicPos";
    }

    private String getAppFileName(String fileName) {
        return sdCardPath + fileName;
    }

    public static JavaCallback getInstance() {
        if (sInstance == null) {
            synchronized (JavaCallback.class) {
                if (sInstance == null) {
                    sInstance = new JavaCallback();
                }
            }
        }
        return sInstance;
    }

    /**
     * ------------------------
     * lcd接口实现
     * ------------------------
     */

    public void ddi_lcd_fill_rect(int left, int top, int right, int bottom, int rgb) {
        VirtualPosScreenView screenView = MainActivity.getScreenView();
        if (screenView == null) {
            return;
        }
        screenView.fillRect(left, top, right, bottom, rgb);
    }

    public void ddi_lcd_clear_rect(int left, int top, int right, int bottom) {
        VirtualPosScreenView screenView = MainActivity.getScreenView();
        if (screenView == null) {
            return;
        }
        screenView.clearRect(left, top, right, bottom);
    }

    public void ddi_lcd_show_text(int x, int y, byte[] data) {
        VirtualPosScreenView screenView = MainActivity.getScreenView();
        if (screenView == null) {
            return;
        }
        String text;
        try {
            text = new String(data, "gb2312");
            screenView.showText(x, y, text);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public void ddi_lcd_show_picture(int left, int top, int right, int bottom, byte[] pic) {
        VirtualPosScreenView screenView = MainActivity.getScreenView();
        if (screenView == null) {
            return;
        }
        screenView.showBitmap(left, top, right - left, bottom - top, pic);
    }

    public void ddi_lcd_brush_screen() {
        VirtualPosScreenView screenView = MainActivity.getScreenView();
        if (screenView == null) {
            return;
        }
        screenView.refreshScreen();
    }


    /**
     * ------------------------
     * key接口实现
     * ------------------------
     */

    public void ddi_key_clear() {
        KeyManager.getsInstance().clear();
    }

    public int ddi_key_read() {
        return KeyManager.getsInstance().poll();
    }


}


